﻿namespace FileCategorizer.Engine
{
    using System.Collections.Specialized;
    using FileCategorizer.Engine.Data;

    /// <summary>
    /// Categorizes file paths based on user-supplied tags.  These tags assist
    /// with searching and displaying groups of files stored in various
    /// locations accessible to the computer.
    /// </summary>
    public class Categorizer
    {
        /// <summary>
        /// The global instance of the Categorizer.
        /// </summary>
        private static Categorizer instance;

        /// <summary>
        /// The database to use for storing tag information.
        /// </summary>
        private IDatabase data;

        /// <summary>
        /// Gets the global instance of the Categorizer.
        /// </summary>
        public static Categorizer Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Categorizer();
                    instance.data = new MemoryDatabase();
                }

                return instance;
            }
        }

        /// <summary>
        /// Gets a value indicating whether there are tagged files in the 
        /// database.
        /// </summary>
        public bool IsEmpty
        {
            get { return this.data.IsEmpty; }
        }

        /// <summary>
        /// Assigns the given tag to the file.
        /// </summary>
        /// <param name="fullPath">The full path to the file.</param>
        /// <param name="tag">The tag to assign to this file.</param>
        public void Categorize(string fullPath, string tag)
        {
            this.data.AddTag(fullPath, tag);
        }

        /// <summary>
        /// Indicates whether the given path has the specified tag.
        /// </summary>
        /// <param name="fullPath">The full path to the file to check.</param>
        /// <param name="tag">The tag to find.</param>
        /// <returns>
        ///     <code>true</code> if the path has had the <code>tag</code> 
        ///     assigned to it.
        /// </returns>
        public bool HasTag(string fullPath, string tag)
        {
            return this.data.HasTag(fullPath, tag);
        }

        /// <summary>
        /// Removes all files and tags from the database.
        /// </summary>
        public void Clear()
        {
            this.data.ClearTags();
        }

        /// <summary>
        /// Searches the tag database for the file name
        /// </summary>
        /// <param name="name">A string in the file path to locate</param>
        /// <returns>A collection of string matching the <code>name</code> substring.</returns>
        public StringCollection SearchByName(string name)
        {
            return this.data.SearchByName(name);
        }

        /// <summary>
        /// Searches the tag database for the file with the specified tag.
        /// </summary>
        /// <param name="tag">The tag to find.</param>
        /// <returns>A collection of file paths mapped to the specified tag.</returns>
        public StringCollection SearchByTag(string tag)
        {
            return this.data.SearchByTag(tag);
        }
    }
}
